<?php

class Availability_model extends MY_Model
{

    function __construct()
    {
        parent::__construct();
        $this->table='availability';

    }

    public function find_stop_booking($start, $end, $room_id)
    {
        $q="SELECT * FROM availability WHERE start <= ? AND end >=? AND rooms_ID=? AND type = 'STOP'";

        $res=$this->db->query($q, array($start, $end, $room_id))->row_array();

        if(count($res) > 0)
            return $res;
        else
            return FALSE;

    }

    function list_by_type($type="")
    {
        $this->db->where('type', $type);
        $res=$this->db->get('availability')->result_array();
        return $res;

    }

    function list_by_room($room_id)
    {
        $this->db->where('rooms_ID', $room_id);
        $this->db->order_by('start', "ASC");
        $this->db->where('type', "STOP");
        $res=$this->db->get('availability')->result_array();
        return $res;

    }

    public function find_overlap($start, $end, $room_id)
    {

        // Legenda uslova
        // strat ili end su izmedju poredjenih
        // start i end su isti sa poredjenim
        // start je isti, a end veci od poredjenog
        // end je isti, a start je manji od poredjenog
        // start je manji od poredjenog, a end je veci od poredjenog

        $q="SELECT * FROM availability WHERE
                (($start>start && $start<end) || ($end>start && $end<end) ||
                    ($start=start && $end=end) ||
                        ($start=start && $end>end) ||
                            ($end=end && $start<start) ||
                                ($start<start && $end>end)) && rooms_ID = $room_id && type = 'STOP'
            ";

        $res=$this->db->query($q)->result_array();

        if(count($res) > 0)
            return TRUE;
        else
            return FALSE;

    }

    public function find_line($start, $end, $room_id)
    {

        // Objasnjenje
        /// Ovdje uzima redove gdje su isti end i strat (baza) i start i end (baza)
        $q="SELECT * FROM availability WHERE end = ? OR start =? AND rooms_ID=? AND type = 'STOP' ORDER BY start ASC";

        $res=$this->db->query($q, array($start, $end, $room_id))->result_array();

        if(count($res) > 0)
            return $res;
        else
            return FALSE;

    }

    public function find_insert_case($start, $end, $line)
    {

        if(count($line) == 2)
            return "INSERT_CASE_3";

        if($end == $line[0]['start'])
            return "INSERT_CASE_2";

        if($start == $line[0]['end'])
            return "INSERT_CASE_1";

        return "ERROR: NO CASE SELECTED!";

    }

    public function find_delete_case($start, $end, $line)
    {

        if($start == $line['start'] && $end == $line['end'])
            return "DELETE_CASE_4";

        if($start > $line['start'] && $end < $line['end'])
            return "DELETE_CASE_3";

        if($start == $line['start'])
            return "DELETE_CASE_2";

        if($end == $line['end'])
            return "DELETE_CASE_1";

        return "ERROR: NO CASE SELECTED!";

    }

    /*
     * Availability calendar
     */

    function find_stop_between_periods($start, $end, $room_id)
    {
        $q="SELECT * FROM availability WHERE start >= ? AND end <=? AND rooms_ID=? AND type = 'STOP'";

        $res=$this->db->query($q, array($start, $end, $room_id))->result_array();

        if(count($res) > 0)
            return $res;
        else
            return FALSE;

    }

}

?>
